

<template>
  <div>
    <div style="width: 100%; height: 80px">
      <el-input
        placeholder="请输入药品编号"
        v-model="input1"
        style="width: 11%; margin: 5px"
        clearable
      >
      </el-input>
      <el-input
        placeholder="请输入药品名称"
        v-model="input"
        style="width: 12%; margin: 20px"
        clearable
      >
      </el-input>
      <el-date-picker
        v-model="value1"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      >
      </el-date-picker>
      <el-button type="primary" icon="el-icon-search" @click="sousuo"
        >搜索</el-button
      >
      <el-button
        type="primary"
        @click="dialogFormVisible = true"
        icon="el-icon-edit"
        >添加</el-button
      >
    </div>
    <el-table
      ref="filterTable"
      :data="
        tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)
      "
      border
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column fixed prop="id" label="药品编号" width="110px" sortable>
      </el-table-column>
      <el-table-column prop="name" label="药品名称" width="100px">
      </el-table-column>
      <el-table-column prop="cost" label="费用" sortable width="100px">
      </el-table-column>
      <el-table-column prop="manufactureDate" label="日期" sortable>
      </el-table-column>
      <el-table-column prop="quality" label="保质期" sortable>
      </el-table-column>

       <el-table-column

        prop="type"
        label="药品型"
        width="100px"
        :filters="[
          { text: '中药', value: '中药' },
          { text: '西药', value: '西药' },
        ]"
        :filter-method="filterTagyao"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope" width="100px">
          <el-tag
            :type="scope.row.type === '中药' ? 'primary' : 'success'"
            disable-transitions
            >{{ scope.row.type }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="batchNumber" label="药品批号" sortable width="120px">
      </el-table-column>
      <el-table-column prop="quantity" label="数量" sortable width="100px">
      </el-table-column>

      <el-table-column prop="handler" label="经办人" width="100px">
      </el-table-column>
     <el-table-column
        prop="company"
        width="100px"
        label="单位"
        :filters="[
          { text: '盒', value: '盒' },
          { text: '克', value: '克' },
          { text: '包', value: '包' },
        ]"
        :filter-method="filterTag"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.company === '克' ? 'primary' : 'success'"
            disable-transitions
            >{{ scope.row.company }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="open(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
      :current-page.sync="page"
    >
    </el-pagination>

    <el-dialog title="药品添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="药品名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="费用" :label-width="formLabelWidth">
         <el-input-number v-model="form.cost" :precision="2" :step="0.1" ></el-input-number>
        </el-form-item>
          <el-form-item label="生产日期-保质日期" :label-width="formLabelWidth">
           <el-date-picker
          v-model="value2"
          type="datetimerange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="药品生产日期"
     
          end-placeholder="药品保质日期"
        >
        </el-date-picker>
        </el-form-item>
       
        <el-form-item label="类型（中，西药）" :label-width="formLabelWidth">
          <el-select v-model="form.type" placeholder="请选择药品类型">
            <el-option label="中药" value="中药"></el-option>
            <el-option label="西药" value="西药"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="数量" :label-width="formLabelWidth">
          <el-input-number v-model="form.quantity" :min="1" :max="10" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="批号" :label-width="formLabelWidth">
          <el-input v-model="form.batchNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经办人" :label-width="formLabelWidth">
          <el-input v-model="form.handler" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位（盒，瓶，包）" :label-width="formLabelWidth">
          <el-select v-model="form.company" placeholder="请选择单位">
            <el-option label="包" value="包"></el-option>
            <el-option label="盒" value="盒"></el-option>
            <el-option label="瓶" value="瓶"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="药品修改" :visible.sync="dialogFormVisibleUpdate">
      <el-form :model="form1">
        <el-form-item label="药品名称" :label-width="formLabelWidth">
          <el-input v-model="form1.name" autocomplete="off"></el-input>
        </el-form-item>
       <el-form-item label="费用" :label-width="formLabelWidth">
         <el-input-number v-model="form1.cost" :precision="2" :step="0.1" ></el-input-number>
        </el-form-item>
         <el-form-item label="生产日期-保质日期" :label-width="formLabelWidth">
        <el-date-picker
      v-model="value3"
      type="daterange"
      :disabled="true"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期">
     </el-date-picker>
        </el-form-item>

        <el-form-item label="药品类型" :label-width="formLabelWidth">
          <el-input  :disabled="true" v-model="form1.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="数量" :label-width="formLabelWidth">
          <el-input  :disabled="true" v-model="form1.quantity" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="药品批号" :label-width="formLabelWidth">
          <el-input  :disabled="true" v-model="form1.batchNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="经办人" :label-width="formLabelWidth">
          <el-input  :disabled="true" v-model="form1.handler" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位" :label-width="formLabelWidth">
          <el-input  :disabled="true" v-model="form1.company" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleUpdate = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import { getList1, getListDrug, add ,update,delect} from "../../../../api/DrugTable";
export default {
  data() {
    return {
      tableData: [],
      input: "",
      value1: "",
      value2:"",
      value3:[],
      input1: "",
      currentPage: 1,
      pagesize: 10,
      dialogFormVisible: false,
      dialogFormVisibleUpdate:false,
      form: {
        name: "",
        cost: "",
        quality: "",
        type: "",
        manufactureDate:"",
        quantity: "",
        batchNumber: "",
        handler: "",
        company: "",
      },
      form1:{
        name: "",
        cost: "",
        quality: "",
        type: "",
        manufactureDate:"",
        quantity: "",
        batchNumber: "",
        handler: "",
        company: "",
      },
      formLabelWidth: "140px",
    };
  },
  mounted() {
    this.Lodwn();
  },
  methods: {
   update() {
      this.dialogFormVisibleUpdate = false;
      update(this.form1).then((res) => {
        if (res.success) {
          this.Lodwn();
          this.form1 = {};
          this.$message({
            message: "修改成功",
            type: "success",
          });
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    handleEdit(index, row) {
      this.dialogFormVisibleUpdate=true
      this.form1=row
      console.log(this.form1)
      this.value3[0]=this.form1.manufactureDate
      this.value3[1]=this.form1.quality
      
        console.log(value3);


      },
    addList() {
      this.dialogFormVisible = false;
      this.form.manufactureDate=this.value2[0]
      this.form.quality=this.value2[1]
      console.log(this.form)
      add(this.form).then((res) => {
        if (res.success) {
          this.Lodwn();
          this.form = {};
          this.$message({
            message: "添加成功", 
            type: "success",
          });
        } else {
          this.$message.error("添加失败");
        }
      });
    },
    change() {
      console.log(this.value1);
    },
     filterTag(value, row) {
      return row.company === value;
    },
    filterTagyao(value, row) {
      return row.type === value;
    },
    handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    Lodwn() {
      getList1().then((res) => {
        if (res.success) {
          this.tableData = res.result;
        }
      });
    },
    sousuo() {
      if (
        (this.input1 == "" || this.input1 == null) &&
        (this.input == "" || this.input == null) &&
        (this.value1 == "" || this.value1 == null)
      ) {
        this.Lodwn();
      } else {
        getListDrug({
          id: this.input1,
          manufactureDate: this.value1,
          name: this.input,
        }).then((res) => {
          this.tableData = res.result;
        });
      }
    },
    open(row) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delect({ id: row.id }).then((res) => {
        if (res.success) {
           this.Lodwn();
          this.$message({
            message: "删除成功",
            type: "success",
          });
        } else {
          this.$message.error("删除失败");
        }
      });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      }
  },
};
</script>
